package ai.people.core.nebula.properties;

import com.vesoft.nebula.client.graph.data.HostAddress;
import com.vesoft.nebula.client.graph.data.SSLParam;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 说明
 *
 * @author zhenglin
 * @since 2022/3/23
 */
@Data
@ConfigurationProperties(prefix = "nebula")
@NoArgsConstructor
@AllArgsConstructor
public class ConnectProperties {

    /**
     * 启用
     */
    private boolean enabled = false;

    /**
     * 连接url
     */
    private List<String> url;

    /**
     * 空间
     */
    private String spaceName;
    /**
     * 元数据空间名称
     */
    private String metaDataName;

    /**
     * 用户名
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * The min connections in pool for all addresses
     */
    private int minConnsSize = 20;

    /**
     * The max connections in pool for all addresses
     */
    private int maxConnsSize = 20;

    /**
     * Socket timeout and Socket connection timeout, unit: millisecond
     */
    private int timeout = 5000;

    /**
    The idleTime of the connection, unit: millisecond
    The connection's idle time more than idleTime, it will be delete
    0 means never delete
     */
    private int idleTime = 500;

    /**
     * the interval time to check idle connection, unit ms, -1 means no check
     */
    private int intervalIdle = 1000;

    /**
     * the wait time to get idle connection, unit ms
     */
    private int waitTime = 1000;

    /**
     * set to true to turn on ssl encrypted traffic
     */
    private boolean enableSsl = false;

    /**
     * ssl param is required if ssl is turned on
     */
    private SSLParam sslParam = null;


    public List<HostAddress> builderUrl() {
        if (CollectionUtils.isEmpty(url)) {
            return null;
        }
        return url.stream().map(x -> new HostAddress(x.split(":")[0], Integer.parseInt(x.split(":")[1]))).collect(Collectors.toList());
    }

}
